//
//  SEGElevatorControlManager.m
//  SEGUhomeIntelligentHardware
//
//  Created by 刘山 on 2018/4/27.
//  Copyright © 2018年 刘山. All rights reserved.
//

//用户所属楼层及电梯组权限查询
#define kQueryGroupFloorByUser [SEGServerManager serverUrlJoint:kClientRootURL,@"/elevator-control-user-api/rest-api/v1/user-elev-group/queryElevGroupList",nil]
// 二维码梯控
#define kGetElevGroupQrCode [SEGServerManager serverUrlJoint:kClientRootURL,@"/elevator-control-user-api/rest-api/v1/user-elev-group/getElevGroupQrCode",nil]

#import "SEGElevatorControlManager.h"

@implementation SEGElevatorControlManager

- (void)requestElevatorControlWithCommunityId:(NSString*)communityId finished:(SEGNetworkReponse)finished {
    NSDictionary *params = @{@"communityId":communityId};
    
    [SEGHTTPBusinessManager seg_requestJSONWithMethod:GET URLString:kQueryGroupFloorByUser parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        NSArray *dataArr;
        if (isSuccessed) {
            // 成功更新本地缓存
            NSDictionary *data = [result valueForKey:@"data"];
            dataArr = data[@"resultList"];
            [SEGElevatorControlManager cache_saveElevatorControlWithData:dataArr];

        } else {
            // 失败取缓存
            dataArr = [SEGElevatorControlManager cache_getElevatorControlData];
        }
        // 数据转模型
        self.dataArr = [NSMutableArray array];
        for (NSDictionary *dic in dataArr) {
            SEGQueryGroupFloorByUserModel *model = [SEGQueryGroupFloorByUserModel yy_modelWithDictionary:dic];
            [self.dataArr addObject:model];
        }
        finished(isSuccessed, result, codeType, errorMessage);
    }];
}

- (void)getElevGroupQrCodeWithElevId:(NSString *)elevId
                               floor:(NSString *)floor
                       directionFlag:(NSString *)directionFlag
                             extDate:(NSString *)extDate
                             openNum:(NSString *)openNum
                            finished:(SEGNetworkReponse)finished {
    NSMutableDictionary *dicM = [@{@"elevId":elevId,
                                   @"floor":floor,
                                   @"directionFlag":directionFlag} mutableCopy];
    if (extDate.length > 0) [dicM setObject:extDate forKey:@"extDate"];
    if (openNum.length > 0) [dicM setObject:openNum forKey:@"openNum"];
    [SEGHTTPBusinessManager seg_requestJSONWithMethod:POST URLString:kGetElevGroupQrCode parameters:dicM finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(isSuccessed,result,codeType,errorMessage);
            return;
        }
        NSDictionary *data = [result valueForKey:@"data"];
        if (!data || ![data isKindOfClass:[NSDictionary class]]) {
            finished(NO,result,codeType,errorMessage);
            return;
        }
        self.qrCodeStr = data[@"result"];
        finished(isSuccessed,result,codeType,errorMessage);
    }];
}

#pragma mark - Cache
// 梯控列表数据缓存
+ (void)cache_saveElevatorControlWithData:(NSArray *)data {
    if (!data || data.count <= 0) return;
    NSString *filePath = [self cache_getCacheDataWithFilePath:[NSString stringWithFormat:@"SEGElevatorControl_%@_%@.data",kAccountCommunityID,kAccountUserID]];
    [NSKeyedArchiver archiveRootObject:data toFile:filePath];
}
+ (NSArray *)cache_getElevatorControlData {
    NSString *filePath = [self cache_getCacheDataWithFilePath:[NSString stringWithFormat:@"SEGElevatorControl_%@_%@.data",kAccountCommunityID,kAccountUserID]];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}
+ (NSString *)cache_getCacheDataWithFilePath:(NSString *)filePath {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    return [documentPath stringByAppendingPathComponent:filePath];
}

@end
